/*
 * Copyright (c) 2011 Obix Labs Limited
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 		Redistribution of source code must retain the above 
 * 		copyright notice, this list of conditions and the 
 * 		following disclaimer.
 *
 * 		Redistribution in binary form must reproduce the 
 * 		above copyright notice, this list of conditions 
 * 		and the following disclaimer in the documentation 
 * 		and/or other materials provided with the distribution.
 * 
 * 	THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF 
 * 	ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS 
 * 	AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
 * 	FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, 
 * 	ARE HEREBY EXCLUDED. OBIX LABS LIMITED ("Obix Labs") AND ITS 
 * 	LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 
 * 	AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
 * 	ITS DERIVATIVES. IN NO EVENT WILL Obix Labs OR ITS LICENSORS BE 
 * 	LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 
 * 	INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 
 * 	DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 
 * 	LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS 
 * 	SOFTWARE, EVEN IF Obix Labs HAS BEEN ADVISED OF THE POSSIBILITY OF 
 * 	SUCH DAMAGES.
 */

package com.obixlabs.commons.io;

import static org.junit.Assert.fail;

import java.io.IOException;

import com.obixlabs.commons.io.InteractiveCommandLineReader;
import com.obixlabs.commons.io.OptionInputHandler;

import junit.framework.Assert;

/*
 * Integration test for InteractiveCommandLineReader and OptionInputHandler. 
 * This is not an automated test as it requires actual command line input/interaction.
 * As such, it has to executed by a human user or command line input will have to be 
 * simulated somehow. 
 */
public class InteractiveCommandLineReaderTst
{
	/*
	 * Test that an input handler can be successfully initialised to accept only
	 * a pre-defined list of values.
	 */
	public void testBasicPrompt()
	{
		String prompt = "Client directory does not exist. Create folder (y/n)?";

		try
		{
			OptionInputHandler inputHandler = new OptionInputHandler(prompt,
					prompt, "Y", "y", "N", "n");
			InteractiveCommandLineReader.prompt(inputHandler);
			String result = inputHandler.getInputValue();
			Assert.assertEquals("Y", result);
		}
		catch (IOException io_exce)
		{
			fail(io_exce.getMessage());
		}
	}

	/*
	 * Tests that when specified, a repeat-prompt is displayed after the first
	 * failure to validate user input.
	 */
	public void testRepeatPrompt()
	{
		String prompt = "Server directory does not exist. Create folder (y/n)?";

		try
		{
			OptionInputHandler inputHandler = new OptionInputHandler(prompt,
					"Please enter either Y or N", "Y", "y", "N", "n");
			InteractiveCommandLineReader.prompt(inputHandler);
			String result = inputHandler.getInputValue();
			Assert.assertEquals("Y", result);
		}
		catch (IOException io_exce)
		{
			fail(io_exce.getMessage());
		}
	}

	/*
	 * Tests that when no restriction is specified, that instances of this type
	 * will accept any non-null and non-empty string.
	 */
	public void testUnrestrictedPrompt()
	{
		String prompt = "Please enter/paste in some random text";

		try
		{
			OptionInputHandler inputHandler = new OptionInputHandler(prompt,
					"Please enter a char sequence");

			InteractiveCommandLineReader.prompt(inputHandler);
			String result = inputHandler.getInputValue();
			System.out.println("\n\n" + result);
		}
		catch (IOException io_exce)
		{
			fail(io_exce.getMessage());
		}
	}

	public static void main(String[] args)
	{
		InteractiveCommandLineReaderTst test = new InteractiveCommandLineReaderTst();
		test.testBasicPrompt();
		test.testRepeatPrompt();
		test.testUnrestrictedPrompt();
	}
}